/*----------------------------------------------------------
* FILE: StrutsCrudAbstractAction.java
* LOGICAL NAME: StrutsCrudAbstractAction
* PRODUCT: jsisgsi-web
*----------------------------------------------------------
*/

package bila.framework.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import bila.framework.commons.Constants;

/**
 * @author CEL
 * <b>Created by </b>WJuarez
 * <b>for </b>jsisgsi-web
 * <b>on </b>23/07/2009 13:49:22
 * <b>Purpose</b> 
 * <p>
 * 		Action General para CRUD
 * </p>
 */
public abstract class StrutsCrudAbstractAction<Dto> extends StrutsQueryAbstractAction<Dto> {
	
	private Log log = LogFactory.getLog(StrutsCrudAbstractAction.class);
	private boolean validateForm;
	
	public abstract void saveServices(Dto dto) throws Exception;
	
	public abstract void updateServices(Dto dto) throws Exception;
	
	public abstract void deleteServices(Dto dto) throws Exception;
	
	public abstract void editModeServices(Dto dto) throws Exception;
	
	/**
	 * Action para guardar un registro 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{		
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);
			saveServices( (Dto) form);
			if(isValidateForm()){
				request.setAttribute(Constants.ACTION_MSG, Constants.SAVE_SUCCESS);
				return getByExample(mapping, form, request, response);
			}else
				request.setAttribute(Constants.ACTION_MSG, Constants.EMPTY_VALIDATOR);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e);
			request.setAttribute(Constants.ACTION_MSG, Constants.SAVE_ERROR);
		}
		return getByExample(mapping, form, request, response);
	}

	/**
	 * Action para actualizar un registro
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward update(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);			
			updateServices( (Dto) form);
			request.setAttribute(Constants.ACTION_MSG, Constants.UPDATE_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e);
			request.setAttribute(Constants.ACTION_MSG, Constants.UPDATE_ERROR);
		}
		return mapping.findForward(Constants.MESSAGE);
	}
	
	/**
	 * Action para eliminar un registro
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);			
			deleteServices( (Dto) form);
			request.setAttribute(Constants.ACTION_MSG, Constants.DELETE_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e);
			request.setAttribute(Constants.ACTION_MSG, Constants.DELETE_ERROR);
		}
		return getByExample(mapping, form, request, response);

	}
	
	/**
	 * Action para cambiar a modo Edicion
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward editMode(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);
			editModeServices((Dto)form);
			return mapping.findForward(Constants.EDIT);
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute(Constants.ACTION_MSG, Constants.EDIT_ERROR);
			log.error(e);			
		}
		return mapping.findForward(Constants.SUCCESS);
	}
	
	public boolean isValidateForm() {
		return validateForm;
	}

	public void setValidateForm(boolean validateForm) {
		this.validateForm = validateForm;
	}

	public Log getLog() {
		return log;
	}

	
}//class
